home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 9.2 KB | 346 lines | [TEXT/MPS ] |
- /*
- File: VirtualFileTests.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #if debug
-
- #ifndef __ABSTRACTFILE__
- #include "AbstractFile.h"
- #endif
-
- #ifndef __RAMFILE__
- #include "RAMFile.h"
- #endif
-
- #ifndef __VIRTUALFILE__
- #include "VirtualFile.h"
- #endif
-
- #ifndef __VVFILEMACFILE__
- #include "VVFileMacFile.h"
- #endif
-
- #ifndef __VIRTUALRAMORDISKFILE__
- #include "VirtualRamOrDiskFile.h"
- #endif
-
- #ifndef __VIRTUALMACFILE__
- #include "VirtualMacFile.h"
- #endif
-
- #ifndef __VIRTUALHFSFILE__
- #include "VirtualHFSFile.h"
- #endif
-
- #ifndef __VIRTUALHFSMACFILE__
- #include "VirtualHFSMacFile.h"
- #endif
-
- #ifndef __VIRTUALFOLDER__
- #include "VirtualFolder.h"
- #endif
-
- #ifndef __VIRTUALENCLOSEDBINARYMACFILE__
- #include "VirtualEnclosedBinaryMacFile.h"
- #endif
-
- #ifndef __VAPPLESINGLEMACFILE__
- #include "VirtualASMacFile.h"
- #endif
-
- #ifndef __FILEOFFILES__
- #include "FileOfFiles.h"
- #endif
-
- #ifndef __FILEINFILE__
- #include "FileInFile.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #ifndef __DEBUGASSERT__
- #include "DebugAssert.h"
- #endif
-
- #ifndef __STRING__
- #include "String.h"
- #endif
-
- #ifndef __THREADUTILITIES__
- #include "ThreadUtilities.h"
- #endif
-
- #ifndef __BUFFER__
- #include "Buffer.h"
- #endif
-
- #pragma segment VirtualFileTests
-
- extern short gBovineTempVRefNum;
- extern long gBovineTempDirID;
-
- /***********************************|****************************************/
-
- Boolean AreSameLength ( const TAbstractFile& a, const TAbstractFile& b )
- {
- long al = 0, bl = 0;
- ASSERT_RETURN_ZERO ( a.GetEnd ( al ) == noErr );
- ASSERT_RETURN_ZERO ( b.GetEnd ( bl ) == noErr );
- return ASSERT ( al == bl );
- }
-
- /***********************************|****************************************/
-
- Boolean AreSamePosition ( const TAbstractFile& a, const TAbstractFile& b )
- {
- long al = 0, bl = 0;
- ASSERT_RETURN_ZERO ( a.GetPosition ( al ) == noErr );
- ASSERT_RETURN_ZERO ( b.GetPosition ( bl ) == noErr );
- return ASSERT ( al == bl );
- }
-
- /***********************************|****************************************/
-
- Boolean HaveSameContent ( const TAbstractFile& ac, const TAbstractFile& bc )
- {
- long ap = 0, bp = 0, al = 0, bl = 0;
-
- ASSERT_RETURN_ZERO ( ac.GetEnd ( al ) == noErr );
- ASSERT_RETURN_ZERO ( bc.GetEnd ( bl ) == noErr );
-
- ASSERT_RETURN_ZERO ( al == bl );
-
- ASSERT_RETURN_ZERO ( ac.GetPosition ( ap ) == noErr );
- ASSERT_RETURN_ZERO ( bc.GetPosition ( bp ) == noErr );
-
- TAbstractFile& a = (TAbstractFile&) ac;
- TAbstractFile& b = (TAbstractFile&) bc;
-
- ASSERT_RETURN_ZERO ( a.SetPosition ( fsFromStart, 0 ) == noErr );
- ASSERT_RETURN_ZERO ( b.SetPosition ( fsFromStart, 0 ) == noErr );
-
- const unsigned long kBufferSize = 1024;
- CBuffer ab ( kBufferSize ), bb ( kBufferSize );
-
- while ( al > 0 )
- {
- long chunkSize = al > kBufferSize ? kBufferSize : al, acs = chunkSize, bcs = chunkSize;
- ASSERT_RETURN_ZERO ( a.ReadDataIgnore ( (void*) ab.GetPhysicalStart (), acs ) == noErr );
- ASSERT_RETURN_ZERO ( acs == chunkSize );
- ASSERT_RETURN_ZERO ( b.ReadDataIgnore ( (void*) bb.GetPhysicalStart (), bcs ) == noErr );
- ASSERT_RETURN_ZERO ( bcs == chunkSize );
- ASSERT_RETURN_ZERO ( ::memcmp ( ab.GetPhysicalStart (), bb.GetPhysicalStart (), (unsigned int) chunkSize ) == 0 );
- al -= chunkSize;
- }
-
- ASSERT_RETURN_ZERO ( a.SetPosition ( fsFromStart, ap ) == noErr );
- ASSERT_RETURN_ZERO ( b.SetPosition ( fsFromStart, bp ) == noErr );
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean AreEqual ( const TAbstractFile& a, const TAbstractFile& b )
- {
- return AreSamePosition ( a, b ) && HaveSameContent ( a, b );
- }
-
- /***********************************|****************************************/
-
- Boolean DoesFileMatch ( TAbstractFile& file, const void* data, unsigned long length )
- {
- TAbstractFile* compare = new TRamFile;
- ASSERT ( compare->WriteDataIgnore ( data, length ) == noErr );
- Boolean areEqual = ASSERT ( HaveSameContent ( file, *compare ) );
- delete compare;
- return areEqual;
- }
-
- /***********************************|****************************************/
-
- Boolean WriteReadTest ( TAbstractFile& file, long length )
- {
- ASSERT_RETURN_ZERO ( file.SetEnd ( 0 ) == noErr );
- ASSERT_RETURN_ZERO ( file.SetPosition ( fsFromStart, 0 ) == noErr );
-
- CBuffer buffer ( &file, length );
- ASSERT_RETURN_ZERO ( file.WriteData ( buffer.GetPhysicalStart (), length ) == noErr );
- ASSERT_RETURN_ZERO ( length == buffer.GetPhysicalLength () );
- ASSERT_RETURN_ZERO ( DoesFileMatch ( file, buffer.GetPhysicalStart (), length ) );
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean EndTest ( TAbstractFile& file, long length )
- {
- long end = 0;
- ASSERT_RETURN_ZERO ( file.SetEnd ( length ) == noErr );
- ASSERT_RETURN_ZERO ( file.GetEnd ( end ) == noErr );
- ASSERT_RETURN_ZERO ( end == length );
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean EndTests ( TAbstractFile& file )
- {
- ASSERT_RETURN_ZERO ( EndTest ( file, 0 ) );
- // ASSERT_RETURN_ZERO ( !EndTest ( file, 0x7FFFFFFF ) );
- // ASSERT_RETURN_ZERO ( !EndTest ( file, -1 ) );
- ASSERT_RETURN_ZERO ( EndTest ( file, 10 * 1024 ) );
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean WriteReadTests ( TAbstractFile& file )
- {
- ASSERT_RETURN_ZERO ( WriteReadTest ( file, 0 ) );
- ASSERT_RETURN_ZERO ( WriteReadTest ( file, 512 ) );
- ASSERT_RETURN_ZERO ( WriteReadTest ( file, 256 ) );
- ASSERT_RETURN_ZERO ( WriteReadTest ( file, 1024 ) );
- ASSERT_RETURN_ZERO ( WriteReadTest ( file, 20 * 1024 ) );
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean PositionTest ( TAbstractFile& file, short mode, long offset )
- {
- long length = 0, position = 0, expected = offset, origPosition = 0;
- ASSERT_RETURN_ZERO ( file.GetEnd ( length ) == noErr );
- ASSERT_RETURN_ZERO ( file.GetPosition ( origPosition ) == noErr );
-
- if ( mode == fsFromMark )
- expected += origPosition;
- else if ( mode == fsFromLEOF )
- expected += length;
-
- ASSERT_RETURN_ZERO ( file.SetPosition ( mode, offset ) == noErr );
- ASSERT_RETURN_ZERO ( file.GetPosition ( position ) == noErr );
- ASSERT_RETURN_ZERO ( position == expected );
- ASSERT_RETURN_ZERO ( file.SetPosition ( fsFromStart, origPosition ) == noErr );
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean PositionTests ( TAbstractFile& file )
- {
- long length = 0;
- ASSERT_RETURN_ZERO ( file.GetEnd ( length ) == noErr );
-
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromStart, 0 ) );
- // ASSERT_RETURN_ZERO ( !PositionTest ( file, fsFromStart, -1 ) );
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromLEOF, 0 ) );
-
- if ( length > 0 )
- {
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromStart, 1 ) );
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromLEOF, -1 ) );
- }
-
- // ASSERT_RETURN_ZERO ( !PositionTest ( file, fsFromLEOF, 1 ) );
-
- if ( length > 1 )
- {
- ASSERT_RETURN_ZERO ( file.SetPosition ( fsFromStart, length / 2 ) == noErr );
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromMark, 0 ) );
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromMark, -1 ) );
- ASSERT_RETURN_ZERO ( PositionTest ( file, fsFromMark, 1 ) );
- }
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean AbstractFileTest ( TAbstractFile& file )
- {
- TRY
- {
- ASSERT_RETURN_ZERO ( EndTests ( file ) );
- ASSERT_RETURN_ZERO ( PositionTests ( file ) );
- ASSERT_RETURN_ZERO ( WriteReadTests ( file ) );
- }
- EXCEPTION
- {
- chris << "\n#### AbstractFileTest Caught Exception" << endl;
- return false;
- }
- ENDEXCEPTION
-
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean TestThenDelete ( TAbstractFile* file )
- {
- ASSERT_RETURN_ZERO ( file != nil );
- ASSERT_RETURN_ZERO ( AbstractFileTest ( *file ) );
- delete file;
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean OpenTestThenDelete ( TVirtualFile* file )
- {
- ASSERT_RETURN_ZERO ( file != nil );
- ASSERT_RETURN_ZERO ( TestThenDelete ( file ) );
- return true;
- }
-
- /***********************************|****************************************/
-
- void VirtualFileTests ()
- {
- TRY
- {
- FSSpec spec;
- spec.vRefNum = gBovineTempVRefNum;
- spec.parID = gBovineTempDirID;
-
- ASSERT ( TestThenDelete ( new TRamFile ) );
-
- PLstrcpy ( spec.name, "\pTForkFile.Data" );
- ASSERT ( TestThenDelete ( new TForkFile ( spec, TAbstractFile::kData ) ) );
-
- PLstrcpy ( spec.name, "\pTForkFile.Resource" );
- ASSERT ( TestThenDelete ( new TForkFile ( spec, TAbstractFile::kResource ) ) );
-
- PLstrcpy ( spec.name, "\pTVirtualHFSFile" );
- ASSERT ( OpenTestThenDelete ( new TVirtualHFSFile ( spec ) ) );
-
- ASSERT ( OpenTestThenDelete ( new TVirtualRamOrDiskFile ( TVirtualRamOrDiskFile::kDisk ) ) );
- ASSERT ( OpenTestThenDelete ( new TVirtualRamOrDiskFile ( TVirtualRamOrDiskFile::kMemory ) ) );
-
- // ASSERT ( OpenTestThenDelete ( new TFileOfFiles ) );
- }
- EXCEPTION
- {
- chris << "\n#### VirtualFileTests Caught Exception" << endl;
- }
- ENDEXCEPTION
- }
-
- /***********************************|****************************************/
-
- #endif // debug
-